package com.wwwarehouse.contract.program_operation.resource_ship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.contract.adapter.ResourceShipAdapter;
import com.wwwarehouse.contract.bean.CustomerAndResBean;
import com.wwwarehouse.contract.bean.CustomerAndResListBean;
import com.wwwarehouse.contract.bean.ResourceShipBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.ResourceShipCheckEvent;
import com.wwwarehouse.contract.program_operation.MyListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/21.
 * 关联客户与资源的fragment
 */

public class ResourceShipFragment extends BaseFragment implements AdapterView.OnItemClickListener {
	private MyListView myListView;
	private ResourceShipAdapter mResourceShipAdapter;
	private List<ResourceShipBean> resourceShipBeanList = new ArrayList<>();
	private boolean isNumOne;
	private CustomerAndResBean mBean;
	int pageNum;
	int CurrentPosition;
	private List<CustomerAndResListBean> listBeen = new ArrayList<>();
	List<CustomerAndResListBean.Customers> mResourceShipCheckedList = new ArrayList<>();
	@Override
	public void setTitle() {
		if (peekFragment() instanceof ResourceShipFragment) {
			mActivity.setTitle(R.string.associate_customers_with_resources);
		}
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.resource_ship_upgrade_fragment, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		TextView mTitleTv = (TextView) view.findViewById(R.id.title_tv);
		mTitleTv.setText("选择客户与提供服务的关系");
		myListView = (MyListView) view.findViewById(R.id.resource_ship_lv);
		myListView.setOnItemClickListener(this);


	}

	@Override
	public void requestDatas() {

	}

	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (enter) {
			if (getArguments() != null) {
				CurrentPosition = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
				if (CurrentPosition == 0) {
					isNumOne = true;
				} else {
					isNumOne = false;
				}
				mBean = (CustomerAndResBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
				 pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
				listBeen.clear();
				for (int i = CurrentPosition * pageNum; i < (CurrentPosition + 1) * pageNum; i++) {
					if (i >= 0 && i < mBean.getListBeen().size()) {
						listBeen.add(mBean.getListBeen().get(i));
					}
				}
			}


			myListView.post(new Runnable() {
				@Override
				public void run() {
					int height = myListView.getHeight();
					mResourceShipAdapter = new ResourceShipAdapter(listBeen, mActivity, height , isNumOne);
					myListView.setAdapter(mResourceShipAdapter);

				}
			});



		}
		return null;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Bundle bundle = new Bundle();
		if (mBean.getListBeen().get(position).getList().size() > 0) {
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_PARA_DATA, (Serializable) mBean.getListBeen().get(CurrentPosition * pageNum + position ).getList());
			bundle.putInt(ContractConstant.KEY_BUNDLE_CUSTOMER_POSITION, CurrentPosition * pageNum + position );
		}
		SelectCustomerFragment selectCustomerFragment = new SelectCustomerFragment();
		selectCustomerFragment.setArguments(bundle);
		pushFragment(selectCustomerFragment);
	}

	/**
	 * 资源关系点击返回的数据
	 * @param resourceShipEvent
	 */
	public void onEventMainThread(ResourceShipCheckEvent resourceShipEvent){
		int position = resourceShipEvent.getPosition();
		List<CustomerAndResListBean.Customers> resourceShipList = resourceShipEvent.getResourceShipList();
		List<CustomerAndResListBean.Customers> list = mBean.getListBeen().get(position).getList();
		list = resourceShipList;
		mResourceShipAdapter.notifyDataSetChanged();

	}


	@Override
	public void onDestroy() {
	        super.onDestroy();
	        EventBus.getDefault().unregister(this);
	    }
}
